package com.eatme.web.sys.controller;

import com.eatme.web.common.vo.AppResponse;
import com.eatme.web.sys.entity.SysResource;
import com.eatme.web.sys.entity.SysRole;
import com.eatme.web.sys.model.RoleVO;
import com.eatme.web.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/role/")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleServiceImpl;



    @RequestMapping( "add")
    @ResponseBody
    public AppResponse add(@RequestBody com.eatme.web.sys.entity.SysRole  sysRole){
        return sysRoleServiceImpl.add(sysRole);
    }

    @RequestMapping( "edit")
    @ResponseBody
    public AppResponse edit(@RequestBody com.eatme.web.sys.entity.SysRole  sysRole) {
        return sysRoleServiceImpl.edit(sysRole);
    }

    @RequestMapping( "delete/{id}")
    @ResponseBody
    public AppResponse delete(@PathVariable(value = "id") Integer id){
        return sysRoleServiceImpl.delete(id);
    }


    @RequestMapping( "list")
    @ResponseBody
    public AppResponse list(){
       List<RoleVO> list = sysRoleServiceImpl.findList();
      return AppResponse.ok().data(list);
    }

    @RequestMapping( "detail/{id}")
    @ResponseBody
    public AppResponse detail(@PathVariable(value = "id") Integer id){
        SysRole sysRole =  sysRoleServiceImpl.getDetail(id);
        return AppResponse.ok().data(sysRole);
    }

    @RequestMapping( "/resource/{id}")
    @ResponseBody
    public AppResponse role(@PathVariable(value = "id") Integer id){
        List<Integer> permission  =  sysRoleServiceImpl.findResource(id);
        return AppResponse.ok().data(permission);
    }
    @RequestMapping( "/change-resource/{id}")
    @ResponseBody
    //@RequiresPermissions("role.distributePermission")
    public AppResponse role(@PathVariable(value = "id" )Integer id, @RequestBody Set<SysResource> resources){
        SysRole sysRole = sysRoleServiceImpl.getDetail(id);
        sysRole.setSysResources(resources);
        AppResponse result = sysRoleServiceImpl.updateRoleResource(sysRole);
        return result;
    }

}
